//
//  BIDCalendarView.m
//  EducationAssistant
//
//  Created by mal on 15/6/18.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDCalendarView.h"
#import "BIDYearAndMonthView.h"
#import "BIDGlobalMethods.h"

@interface BIDCalendarView()
{
    int _curYear;
    int _curMonth;
    int _trueYear;
    int _trueMonth;
    BIDYearAndMonthView *_dateView;
    NSMutableArray *_labelArr;
}

@end

@implementation BIDCalendarView
@synthesize delegate;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)initView
{
    [self initWeekHeader];
    //
    _labelArr = [[NSMutableArray alloc] init];
    //
    _dateView = [[BIDYearAndMonthView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
    //
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    [_dateLabel addGestureRecognizer:tapGestureRecognizer];
    
    //今天的日期
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //根据今天的日期获取日期组件
    NSDateComponents *todayComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:today];
    _curMonth = (int)todayComponents.month;
    _curYear = (int)todayComponents.year;
    _trueMonth = _curMonth;
    _trueYear = _curYear;
    _dateLabel.text = _curMonth<10?[[NSString alloc] initWithFormat:@"%d-0%d", _trueYear, _trueMonth]:[[NSString alloc] initWithFormat:@"%d-%d", _trueYear, _trueMonth];
    [self initLayout:_curMonth year:_curYear];
    //
    UISwipeGestureRecognizer *leftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeFromLeftToRight:)];
    leftToRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    //
    UISwipeGestureRecognizer *rightToLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeFromRightToLeft:)];
    rightToLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    //
    [self addGestureRecognizer:leftToRightGesture];
    [self addGestureRecognizer:rightToLeftGesture];
}
/**
 *显示week day
 */
- (void)initWeekHeader
{
    CGFloat leftPadding = 2.5f;
    CGFloat rightPadding = 2.5f;
    NSArray *weekArr = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    CGFloat labelWidth = ([UIScreen mainScreen].bounds.size.width-leftPadding-rightPadding)/7;
    CGFloat labelHeight = 30.0f;
    CGPoint startPt = CGPointMake(leftPadding, CGRectGetHeight(_dateBarView.frame));
    for(int i=0; i<weekArr.count; i++)
    {
        CGRect labelFrame = CGRectMake(startPt.x+i*labelWidth, startPt.y, labelWidth, labelHeight);
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
        label.font = [UIFont systemFontOfSize:15.0f];
        if(i==0 || i==6)
        {
            label.textColor = [UIColor colorWithRed:230.0f/255.0f green:144.0f/255.0f blue:114.0f/255.0f alpha:1.0f];
        }
        else
        {
            label.textColor = [UIColor blackColor];
        }
        label.text = weekArr[i];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
    }
}

- (void)initLayout:(int)month year:(int)year
{
    for(UILabel *label in _labelArr)
    {
        [label removeFromSuperview];
    }
    [_labelArr removeAllObjects];
    CGFloat leftPadding = 2.5f;
    CGFloat rightPadding = 2.5f;
    CGPoint startPt = CGPointMake(leftPadding, 44.0f+30);
    CGFloat itemSpacing = 0.0f;
    CGFloat lineSpacing = 0.0f;
    CGFloat sideLength = ([UIScreen mainScreen].bounds.size.width-leftPadding-rightPadding)/7;
    CGSize labelSize = CGSizeMake(sideLength, sideLength);
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //今天的日期
    NSDate *today = [NSDate date];
    //根据今天的日期获取日期组件
    NSDateComponents *todayComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:today];
    //设置本月的1号的dateComponents
    [todayComponents setDay:1];
    [todayComponents setMonth:month];
    [todayComponents setYear:year];
    NSDate *firstDateInMonth = [calendar dateFromComponents:todayComponents];
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:firstDateInMonth];
    //获取本月1号是周几
    NSInteger weekDay = [weekdayComponents weekday];
    //获取本月共有多少天
    NSInteger monthDays = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDateInMonth].length;
    //创建日历
    for(int i=1; i<monthDays+1; i++)
    {
        //if(i<weekDay) continue;
        CGPoint pt;
        pt.x = startPt.x + (i+weekDay-2)%7 * labelSize.width + (i+weekDay-2)%7 * itemSpacing;
        pt.y = startPt.y + (i+weekDay-2)/7 * labelSize.height + (i+weekDay-2)/7 *lineSpacing;
        CGRect labelFrame = CGRectMake(pt.x, pt.y, labelSize.width, labelSize.height);
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
        [label setFont:[UIFont systemFontOfSize:13.0f]];
        [label setTextAlignment:NSTextAlignmentCenter];
        if((i+weekDay-2)%7 == 0 || (i+weekDay-2)%7 == 6)
        {
            //周末显示红色
            [label setTextColor:[UIColor colorWithRed:230.0f/255.0f green:144.0f/255.0f blue:114.0f/255.0f alpha:1.0f]];
        }
        else
        {
            [label setTextColor:[UIColor colorWithRed:148.0f/255.0f green:192.0f/255.0f blue:224.0f/255.0f alpha:1.0f]];
        }
        [label setText:[[NSString alloc] initWithFormat:@"%d", i]];
        label.tag = i;
        label.userInteractionEnabled = YES;
        label.layer.borderColor = [UIColor lightGrayColor].CGColor;
        label.layer.borderWidth = 0.5f;
        //UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
        //[label addGestureRecognizer:tapGesture];
        [self addSubview:label];
        [_labelArr addObject:label];
    }
    //获得该view的最终高度
    UILabel *lastLabel = [_labelArr lastObject];
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(lastLabel.frame);
    self.frame = ownFrame;
    [self setBackgroundColor:[UIColor colorWithRed:247.0f/255.0f green:247.0f/255.0f blue:247.0f/255.0f alpha:1.0f]];
    //考勤信息
    [delegate updateCalendarView:[[NSString alloc] initWithFormat:@"%d", year] month:[[NSString alloc] initWithFormat:@"%d", month]];
    //
    //NSString *strDate = month<10?[[NSString alloc] initWithFormat:@"%d-0%d", year, month]:[[NSString alloc] initWithFormat:@"%d-%d", year, month];
}

/**
 *不同的缺勤类型，label对应不同的背景色
 */
- (void)setLabelBgColor:(UIColor *)color index:(NSUInteger)index
{
    UILabel *label = (UILabel*)_labelArr[index];
    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:color];
}
/**
 *显示日期选项
 */
- (void)tapGestureHandler:(UITapGestureRecognizer*)gestureRecognizer
{
}

/**
 *上一个月
 */
- (IBAction)leftBtnHandler:(id)sender
{
    _curMonth--;
    if(_curMonth==0)
    {
        _curMonth = 12;
        _curYear--;
    }
    _dateLabel.text = _curMonth<10?[[NSString alloc] initWithFormat:@"%d-0%d", _curYear, _curMonth]:[[NSString alloc] initWithFormat:@"%d-%d", _curYear, _curMonth];
    [self initLayout:_curMonth year:_curYear];
}
/**
 *下一个月
 */
- (IBAction)rightBtnHandler:(id)sender
{
    //现实中今天的月份
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit fromDate:today];
    int month = components.month;
    int year = components.year;
    if(_curMonth == month && year==_curYear)
    {
        //[BIDGlobalMethods showAlertView:@"已经是当前月了" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }
    //
    _curMonth++;
    if(_curMonth>12)
    {
        _curMonth = 1;
        _curYear++;
    }
    _dateLabel.text = _curMonth<10?[[NSString alloc] initWithFormat:@"%d-0%d", _curYear, _curMonth]:[[NSString alloc] initWithFormat:@"%d-%d", _curYear, _curMonth];
    [self initLayout:_curMonth year:_curYear];
}

- (void)swipeFromLeftToRight:(UISwipeGestureRecognizer*)gestureRecognizer
{
    _curMonth--;
    if(_curMonth==0)
    {
        _curMonth = 12;
        _curYear--;
    }
    _dateLabel.text = _curMonth<10?[[NSString alloc] initWithFormat:@"%d-0%d", _curYear, _curMonth]:[[NSString alloc] initWithFormat:@"%d-%d", _curYear, _curMonth];
    [self initLayout:_curMonth year:_curYear];
}

- (void)swipeFromRightToLeft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //现实中今天的月份
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit fromDate:today];
    int month = components.month;
    int year = components.year;
    if(_curMonth == month && year==_curYear)
    {
        //[BIDGlobalMethods showAlertView:@"已经是当前月了" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }
    //
    _curMonth++;
    if(_curMonth>12)
    {
        _curMonth = 1;
        _curYear++;
    }
    _dateLabel.text = _curMonth<10?[[NSString alloc] initWithFormat:@"%d-0%d", _curYear, _curMonth]:[[NSString alloc] initWithFormat:@"%d-%d", _curYear, _curMonth];
    [self initLayout:_curMonth year:_curYear];
}

@end
